import Model from '../abstract';
import ModelCatalogProduct from './product';
import downloadFiles, { downloadFile } from '../../utils/downImgUtils';

export default class ModelCatalogCategory extends Model {
      
      constructor() {
            super();
            this.mainTable = 'category';
            this.pk = 'category_id';
            this.serverPk = 'server_category_id';
            this.versionName = 'version';
            this.dateAddedName = 'date_added';
            this.lastUpdatedName = 'last_updated';
            this.modelProduct = new ModelCatalogProduct();
      }

      /**
       * 获取分类商品列表
       */
      async getCategoryProducts(start = 0, limit = 0) {
            let rows = await this.query(start, limit);
            let categories = [];
            for (row of rows) {
                  let categoryId = row.category_id;
                  let num = await this.countProducts(categoryId);
                  if (num > 0) {
                        row.products = await this.modelProduct.getProductsByCategory(categoryId, start, limit);
                        categories.push(row);
                  }
            }
            return categories;
      }

      /**
       * 检测分类是否有商品
       */
      async countProducts(categoryId) {
            return await this.numRows("product_to_category", {category_id: categoryId});
      }

      /**
       * 清除分类数据
       */
      async clean() {
            return await this.delete(this.mainTable);
      }

      /**
       * 删除商品分类
       */
      async removeCategory(categoryId) {
            let category = await this.get(categoryId);
            if (category) {
                  // 删除图片
                  if (category.image) {
                  }
                  // 删除商品分类对照表
                  await this.delete("product_to_category", {category_id: categoryId});
                  // 删除本条商品分类数据
                  return await this.remove(categoryId);
            }
            return false;
      }

      /**
       * 删除商品分类
       */
      async removeCategoryByServerPk(serverPk) {
            let categoryId = await this.getIdByServerPk(serverPk);
            if (categoryId > 0) {
                  return await this.removeCategory(categoryId);
            }
            return false;
      }

      /**
       * 保存图片
       */
      async saveImage(categoryId, image) {
            let data = {
                  image: image
            };
            return await this.update(this.mainTable, data, {category_id: categoryId});
      }

      /**
       * 数据导入
       */
      async download(rows = []) {
            if (!Array.isArray(rows)) {
                  rows = [];
            }

            let localServerPks = await this.getServerPks();
            let remoteServerPks = [];
            for (row of rows) {
                  let serverCategoryId = row.server_category_id;
                  if (serverCategoryId > 0) {
                        remoteServerPks.push(serverCategoryId);
                        let version = await this.getVersionByServerPk(serverCategoryId);
                        if (version < row.version) {
                              let image = null;
                              if (row.image) {
                                    image = row.image;
                                    delete row.image;
                              }
                              let categoryId = await this.saveByServerPk(row, serverCategoryId);
                              if (categoryId > 0 && image) {
                                    let name = sprintf("category/%d", categoryId);
                                    let file = await downloadFile(image, name);
                                    if (file !== null) {
                                          console.log("category image file >>>> ", file);
                                          await this.saveImage(categoryId, file);
                                    }
                              }
                        }
                  }
            }

            let delServerPks = localServerPks.filter(el => !remoteServerPks.includes(el));
            for (serverPk of delServerPks) {
                  if (serverPk > 0) {
                        await this.removeCategoryByServerPk(serverPk);
                  }
            }
            return true;
      }

      /**
       * 数据上传
       */
      async upload() {
            return true;
      }
      
}